﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Hongsh.Pay.Client {

    public static class StoryboardExtends {

        public static Storyboard Fade(this FrameworkElement src, int duration_in, int begin_time_in, int duration_out, int begin_time_out) {
            Storyboard storyboard = new Storyboard {
                SpeedRatio = 1.2
            };
            if (duration_in > 0) {
                DoubleAnimation doubleAnimation = new DoubleAnimation {
                    From = new double?(0.0),
                    To = new double?(1f),
                    Duration = new Duration(TimeSpan.FromMilliseconds(duration_in)),
                    BeginTime = new TimeSpan?(TimeSpan.FromMilliseconds(begin_time_in))
                };
                doubleAnimation.CurrentTimeInvalidated += delegate (object sender, EventArgs e) {
                    src.Visibility = Visibility.Visible;
                };
                storyboard.Children.Add(doubleAnimation);
            }
            if (duration_out > 0) {
                DoubleAnimation doubleAnimation2 = new DoubleAnimation {
                    From = new double?(1f),
                    To = new double?(0.0),
                    Duration = new Duration(TimeSpan.FromMilliseconds(duration_out)),
                    BeginTime = new TimeSpan?(TimeSpan.FromMilliseconds(duration_in + begin_time_in + begin_time_out))
                };
                doubleAnimation2.Completed += delegate (object sender, EventArgs e) {
                    if (src.Opacity == 0.0) {
                        src.Visibility = Visibility.Collapsed;
                    }
                };
                storyboard.Children.Add(doubleAnimation2);
            }
            Storyboard.SetTargetProperty(storyboard, new PropertyPath(UIElement.OpacityProperty));
            Storyboard.SetTarget(storyboard, src);
            return storyboard;
        }


        public static Storyboard FadeIn(this FrameworkElement src, int duration, int begin_time = 0) {
            return src.Fade(duration, begin_time, 0, 0);
        }


        public static Storyboard FadeOut(this FrameworkElement src, int duration, int begin_time = 0) {
            return src.Fade(0, 0, duration, begin_time);
        }


        public static Storyboard Spin(this FrameworkElement src, double from, double to, int duration, int begin_time = 0, int? repeat = null, double center_x = 0.5, double center_y = 0.5) {
            Storyboard storyboard = new Storyboard();
            DoubleAnimation doubleAnimation = new DoubleAnimation(from, to, new Duration(TimeSpan.FromMilliseconds(duration))) {
                BeginTime = new TimeSpan?(TimeSpan.FromMilliseconds(begin_time))
            };
            doubleAnimation.RepeatBehavior = (repeat == null) ? RepeatBehavior.Forever : new RepeatBehavior(TimeSpan.FromMilliseconds(repeat.Value));
            src.RenderTransform = new RotateTransform();
            src.RenderTransformOrigin = new Point(center_x, center_y);
            storyboard.Children.Add(doubleAnimation);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("RenderTransform.Angle", new object[0]));
            Storyboard.SetTarget(storyboard, src);
            return storyboard;
        }

        // Token: 0x0600003B RID: 59 RVA: 0x000037F4 File Offset: 0x000019F4
        public static Storyboard ZoomOut(this FrameworkElement src, int duration, int begin_time = 0, double center_x = 0.5, double center_y = 0.5) {
            FrameworkElement frameworkElement = (FrameworkElement)src.Parent;
            Storyboard storyboard = new Storyboard();
            Duration duration2 = new Duration(TimeSpan.FromMilliseconds(duration * 0.5));
            Duration duration3 = new Duration(TimeSpan.FromMilliseconds(duration * 0.5));
            TimeSpan value = TimeSpan.FromMilliseconds(begin_time);
            TimeSpan value2 = TimeSpan.FromMilliseconds(begin_time + duration * 0.5);
            src.RenderTransform = new ScaleTransform();
            src.RenderTransformOrigin = new Point(center_x, center_y);
            DoubleAnimation doubleAnimation = new DoubleAnimation {
                To = new double?(1.5),
                Duration = duration2,
                BeginTime = new TimeSpan?(value)
            };
            storyboard.Children.Add(doubleAnimation);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("RenderTransform.ScaleX", new object[0]));
            DoubleAnimation doubleAnimation2 = new DoubleAnimation {
                To = new double?(1.5),
                Duration = duration2,
                BeginTime = new TimeSpan?(value)
            };
            storyboard.Children.Add(doubleAnimation2);
            Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath("RenderTransform.ScaleY", new object[0]));
            DoubleAnimation doubleAnimation3 = new DoubleAnimation {
                To = new double?(0.0),
                Duration = duration3,
                BeginTime = new TimeSpan?(value2)
            };
            storyboard.Children.Add(doubleAnimation3);
            Storyboard.SetTargetProperty(doubleAnimation3, new PropertyPath("RenderTransform.ScaleX", new object[0]));
            DoubleAnimation doubleAnimation4 = new DoubleAnimation {
                To = new double?(0.0),
                Duration = duration3,
                BeginTime = new TimeSpan?(value2)
            };
            storyboard.SpeedRatio = 1.5;
            storyboard.Children.Add(doubleAnimation4);
            Storyboard.SetTargetProperty(doubleAnimation4, new PropertyPath("RenderTransform.ScaleY", new object[0]));
            Storyboard.SetTarget(storyboard, src);
            storyboard.CurrentTimeInvalidated += delegate (object sender, EventArgs e) {
                src.Visibility = Visibility.Visible;
            };
            storyboard.Completed += delegate (object sender, EventArgs e) {
                src.Visibility = Visibility.Collapsed;
            };
            return storyboard;
        }
    }
}
